﻿import flash.geom.Point;

class com.embedchat.skins.standart.scrollbar.ScrollBarClass extends MovieClip{
	static var instanceName:String = new String ("ScrollBarClass");
	static var instanceOwner:Object = com.embedchat.skins.standart.scrollbar.ScrollBarClass;
	[Inspectable(name="Scrollbar direction", defaultValue="vertical",type:String, enumeration="vertical,horizontal")]
	public var direction:String;
	[Inspectable(name="Scrollbar width",defaultValue=16,type:Number)]
	public var canvasWidth:Number;
	[Inspectable(name="Scrollbar height",defaultValue=50,type:Number)]	
	public var canvasHeight:Number;
	[Inspectable(name="Button width",defaultValue=16,type:Number)]
	public var buttonWidth:Number;
	[Inspectable(name="Button height",defaultValue=16,type:Number)]	
	public var buttonHeight:Number;
	
	private var broadcasterObj:Object = null;
	private var sliderOldPosition:Point;
	
	
	private var buttonUp:MovieClip = null;
	private var buttonDown:MovieClip = null;
	private var buttonLeft:MovieClip = null;
	private var buttonRight:MovieClip = null;	
	private var track:MovieClip = null;	
	private var slider:MovieClip = null;		
	private var buttonSize:Point;
	private var scrollRealSize:Point;
	
	function ScrollBarClass(){
		this.buttonSize = new Point(this.buttonWidth,this.buttonHeight);
		this.scrollRealSize = new Point(this.canvasWidth-2*(this.buttonSize.x+1),this.canvasHeight-2*(this.buttonSize.y+1));
		switch(this.direction){
			case "vertical":
				this.initVerticalBar();
			break;
			case "horizontal":
				this.initHorizontalBar();
			break;
		}
	}
	
	private function initVerticalBar():Void{
		this.track = this.initTrack("track");
		this.buttonUp = this.initButton("buttonUp","up");
		this.buttonDown = this.initButton("buttonDown","down");
		this.slider = this.initSlider("slider");
	}
	private function initHorizontalBar():Void{
		this.track = this.initTrack("track");
		this.buttonLeft = this.initButton("buttonLeft","left");
		this.buttonRight = this.initButton("buttonRight","right");		
		this.slider = this.initSlider("slider");
	}
	
	private function initTrack(movieName:String):MovieClip{
		return this.attachMovie("ScrollBarTrack",movieName,this.getNextHighestDepth(),{direction:this.direction,canvasWidth:this.canvasWidth,canvasHeight:this.canvasHeight, _x:0, _y:0});
	}

	private function initSlider(movieName:String):MovieClip{
		var start:Point;
		var end:Point;
		switch(this.direction){
			case "vertical":
				start = new Point((this.canvasWidth-this.buttonSize.x)/2,this.buttonSize.y);
				end = new Point(this.buttonSize.x,this.canvasHeight - 2*this.buttonSize.y);			
			break;
			case "horizontal":
				start = new Point(this.buttonSize.x,(this.canvasHeight-this.buttonSize.y)/2);
				end = new Point(this.canvasWidth - 2*this.buttonSize.x,this.buttonSize.y);
			break;
			default:
				return null;
		}
		return this.attachMovie("ScrollBarSlider",movieName,this.getNextHighestDepth(),{direction:this.direction,canvasWidth:end.x,canvasHeight:end.y, _x:start.x, _y:start.y, broadcasterObj:this.broadcasterObj});
		
	}	
	
	private function initButton(movieName:String, direction:String):MovieClip{
		var buttonPosition:Point;
		switch(direction){
			case "up":
				buttonPosition = new Point((this.canvasWidth-this.buttonSize.x)/2,0)
			break;
			case "down":
				buttonPosition = new Point((this.canvasWidth-this.buttonSize.x)/2,(this.canvasHeight-this.buttonSize.y));
			break;
			case "left":
				buttonPosition = new Point(0,(this.canvasHeight-this.buttonSize.y)/2);
			break;
			case "right":
				buttonPosition = new Point((this.canvasWidth-this.buttonSize.x),(this.canvasHeight-this.buttonSize.y)/2);
			break;
			default:
				return null;
			break;
		}
		
		return this.attachMovie("ScrollBarButton",movieName,this.getNextHighestDepth(),{arrawDirection:direction,arrowCircleRadius:3,canvasWidth:this.buttonSize.x,canvasHeight:this.buttonSize.y, _x:buttonPosition.x, _y:buttonPosition.y,  broadcasterObj:this.broadcasterObj});
	}

	public function updateTextVerticalScrollbar(textField:TextField):Void{
		var minSliderSize:Number = 8;
		var lineHeight:Number = 2;		
		var allowedSpace:Number =  this.canvasHeight - 2*this.buttonHeight;
		var textFieldTextFormat:TextFormat = textField.getTextFormat();

		var stringHeight:Number = lineHeight + textFieldTextFormat.size;
		var heightInSybols:Number = Math.floor(textField._height/stringHeight)*stringHeight;

		var newSize:Number = Math.floor(allowedSpace*heightInSybols/textField.textHeight);
		newSize = (newSize>minSliderSize)?newSize:minSliderSize;
		var preY:Number = Math.floor((allowedSpace)*(textField.scroll*stringHeight-stringHeight)/textField.textHeight+this.buttonHeight+1);
		var newY:Number = ((preY+newSize)<(this.canvasHeight - this.buttonHeight/*84*/))?preY:((this.canvasHeight - this.buttonHeight/*84*/)-newSize-1);
		
		if(this.slider){
			this.slider.updateVerticalSlider(newY,newSize);
		}
	}
	
	public function setSliderPosition(newPos:Point):Void{
		this.sliderOldPosition = newPos;
	}
	
	public function updateVerticalSlider(newPos:Point):Void{
		var newY:Number =  this.slider._y + (newPos.y-this.sliderOldPosition.y);
		
		if(newY<=(this.buttonHeight+1)){
			this.slider._y = (this.buttonHeight+1);
		}else if((newY + this.slider._height) >= (this.canvasHeight - (this.buttonHeight+1))){
			this.slider._y = (this.canvasHeight - (this.buttonHeight+1) - this.slider._height);
		}else{
			this.slider._y = newY;
		}
		this.setSliderPosition(newPos);
		if(this.broadcasterObj){
			var scrollTrackBefore:Point = new Point(this.slider._x-(this.buttonSize.x+1),this.slider._y-(this.buttonSize.y+1));
			var currentSliderSize:Point = new Point(this.slider.canvasWidth,this.slider.canvasHeight);
			this.broadcasterObj.broadcastMessage("onSinhronizeScroll",this,scrollTrackBefore,scrollRealSize,currentSliderSize);
		}
	}
}